package com.aierjun.testservice;

import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/29.
 */
public class CaseInForEntity {
    private String nu;
    private String com;
    private String ischeck;
    private String status;
    private List<LogisticsInForEntity> data;

    private static final String KEY_NU = "nu";
    private static final String KEY_COM = "com";
    private static final String KEY_ISCHECK = "ischeck";
    private static final String KEY_STATUS = "status";
    private static final String KEY_DATA = "data";

    @Override
    public String toString() {
        return "CaseInForEntity{" +
                "nu='" + nu + '\'' +
                ", com='" + com + '\'' +
                ", ischeck='" + ischeck + '\'' +
                ", status='" + status + '\'' +
                ", data=" + data +
                '}';
    }

    public static CaseInForEntity parseFromJsonStr(String jsonStr) {
        CaseInForEntity caseInForEntity = new CaseInForEntity();
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            caseInForEntity.nu = jsonObject.getString(KEY_NU);
            caseInForEntity.com = jsonObject.optString(KEY_COM);
            caseInForEntity.ischeck = jsonObject.optString(KEY_ISCHECK);
            caseInForEntity.status = jsonObject.optString(KEY_STATUS);
            JSONArray jsonArray = jsonObject.optJSONArray(KEY_DATA);
            if (jsonArray.length() > 0) {
                List<LogisticsInForEntity> logisticsInForEntities = new ArrayList<>();
                for (int i = 0; i < jsonArray.length(); i++) {
                    logisticsInForEntities.add(LogisticsInForEntity.parseFromJsonObj(jsonArray.getJSONObject(i)));
                }
                caseInForEntity.data = logisticsInForEntities;
            }
        } catch (JSONException e) {
            return caseInForEntity;
        }
        return caseInForEntity;
    }

    static class LogisticsInForEntity {
        private String time;
        private String context;
        private String ftime;

        @Override
        public String toString() {
            return "LogisticsInForEntity{" +
                    "time='" + time + '\'' +
                    ", context='" + context + '\'' +
                    ", ftime='" + ftime + '\'' +
                    '}';
        }

        private static final String KEY_TIME = "time";
        private static final String KEY_CONTEXT = "context";
        private static final String KEY_FTIME = "ftime";

        public static LogisticsInForEntity parseFromJsonObj(JSONObject jsonObject) {
            LogisticsInForEntity logisticsInForEntity = new LogisticsInForEntity();
            try {
                logisticsInForEntity.time = jsonObject.getString(KEY_TIME);
                logisticsInForEntity.context = jsonObject.getString(KEY_CONTEXT);
                logisticsInForEntity.ftime = jsonObject.getString(KEY_FTIME);
            } catch (JSONException e) {
                Log.e("aierjun", "解析错误");
                return null;
            }
            return logisticsInForEntity;
        }
    }
}